<template>
	<el-dialog :title="titleShow" :visible.sync="vm.dialogFormVisible" @open="openHandle()" @close="closeHandle()">
		<el-form ref="form" :model="form" :rules="rules">
			<el-form-item label="权限名称" :label-width="formLabelWidth" prop="aname">
				<el-input v-model="form.aname" autocomplete="off"></el-input>
			</el-form-item>
			<el-form-item label="权限描述" :label-width="formLabelWidth" prop="desc">
				<el-input v-model="form.desc" autocomplete="off"></el-input>
			</el-form-item>
		</el-form>
		<div slot="footer" class="dialog-footer">
			<el-button @click="vm.dialogFormVisible = false">取 消</el-button>
			<el-button type="primary" @click="handle()" :loading="loading">{{this.vm.isAdd?'添加':'修改'}}</el-button>
		</div>
	</el-dialog>
</template>

<script>
	export default ({
		props: ['vm'],
		data() {
			return {
				form: {
					aname: '',
					desc: ''
				},
				formLabelWidth: '150px',
				loading: false,
				rules: {
					aname: [{
						required: true,
						message: '权限名称不能为空',
						trigger: 'change'
					}],
					desc: [{
						required: true,
						message: '权限描述不能为空',
						trigger: 'change'
					}]
				}
				
			}
		},
		methods: {
			openHandle() {
				if (!this.vm.isAdd) {
					this.axios({
						method: 'post',
						url: 'http://localhost:8080/auth/find',
						data: {
							authid: this.vm.selAuthid
						},
						headers: {
							'Content-Type': 'application/json;charset=utf-8'
						}
					}).then((response) => {
						if (response.data) {
							this.form.aname = response.data.aname
							this.form.desc = response.data.desc
						}
					})
				}
			},
			closeHandle() {
				this.$refs.form.resetFields()
			},
			handle() {
				this.$refs.form.validate((valid) => {
					if (valid) {
						this.loading = true
						if (this.vm.isAdd) {
							this.add()
						} else {
							this.update()
						}
					}
				})
			},
			add() {
				this.axios({
					method: 'post',
					url: 'http://localhost:8080/auth/add',
					data: this.form,
					headers: {
						'Content-Type': 'application/json;charset=utf-8'
					}
				}).then((response) => {
					if (response.data && response.data.code === 1000) {
						this.$message.success('添加成功')
					} else {
						this.$message.error('添加失败')
					}
					this.vm.fetchData()
					this.vm.dialogFormVisible = false
					this.loading = false
				}).catch((err) => {
					this.$message.error('添加失败')
					this.vm.fetchData()
					this.vm.dialogFormVisible = false
					this.loading = false
				})
			},
			update() {
				this.axios({
					method: 'post',
					url: 'http://localhost:8080/auth/update',
					data: {
						...this.form,
						authid: this.vm.selAuthid
					},
					headers: {
						'Content-Type': 'application/json;charset=utf-8'
					}
				}).then((response) => {
					if (response.data && response.data.code === 1000) {
						this.$message.success('修改成功')
					} else {
						this.$message.error('修改失败')
					}
					this.vm.fetchData()
					this.vm.dialogFormVisible = false
					this.loading = false
				}).catch((err) => {
					this.$message.error('修改失败')
					this.vm.fetchData()
					this.vm.dialogFormVisible = false
					this.loading = false
				})
			}
		},
		computed: {
			titleShow() {
				return this.vm.isAdd ? '添加权限' : '修改权限'
			}
		},

	})
</script>
